/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-11-29 15:32:07
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-05-09 18:05:03
 * @FilePath: /yt-devops/lib/pages/tabs/device_tab/device_tab_controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:native_dart_package/http/request/exception/exception_handler.dart';
import 'package:native_dart_package/native_dart_package.dart';
import 'package:yt_devops/components/app_permission/yt_tab_view_model.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/pages/alarm/alarm_home/alarm_home_view.dart';
import 'package:yt_devops/pages/devices/device_list/device_list_model.dart';
import 'package:yt_devops/pages/devices/device_tab/basic_info/basic_info/basic_info_view.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/new_air_cooling_page/new_air_cooling_page_controller.dart';
import 'package:yt_devops/pages/devices/device_tab/history_info/history_info_view.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/site_mains_elec_detail/keep_aliver_wrapper.dart';
import 'package:yt_devops/pages/tabs/device_tab/device_tab_model.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/device/device_sevices.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import 'package:yt_devops/utils/enums.dart';
import '../../../components/log_utils.dart';

class DeviceTabController extends GetxController
    with GetTickerProviderStateMixin {
  DeviceServices services = Get.put(DeviceServices());

  //
  DeviceTabModel? model;
  // 产品类型
  ProductType? type;
  // 设备id
  late int deviceId;
  // 产品id类型
  ProductIDType? productId;
  // 设备名称
  late String name;
  // 设备详情
  DeviceModel? deviceDetailModel;

  // 设备树
  DeviceListModel? deviceTree;

  TabController? tabController;
  ConnectStatusType? connectStatus;
  List<int> subDevicesIdList = []; //设备详情ems需要订阅的子设备
  List<String> subDevicesNameList = []; //设备详情ems需要订阅的子设备名称
  int currentIndex = 0;

  Widget? detailWidget;
  Widget? alarmHomeWidget;
  Widget? baseInfoWidget;
  List<Widget> sitePages = [];
  List<BottomNavigationBarItem> items = [];
  List<YtTabViewModel> permissionGroups = [];

  @override
  void onInit() {
    super.onInit();
    if (Get.arguments != null) {
      deviceTree = Get.arguments['deviceTree'];
      deviceId = Get.arguments['deviceId'];
      Log.d("设备模块Tab，值为" + Get.arguments.toString());
      name = Get.arguments['deviceName'];
      type = ProductType.values.firstWhereOrNull(
          (element) => element.value == Get.arguments['type']);
      productId = ProductIDType.values.firstWhereOrNull(
          (element) => element.value == Get.arguments['productId']);
      if (Get.arguments['productId'] != null) {
        // productId =  ProductIDType(Get.arguments['productId'] );
      }
      connectStatus = Get.arguments['connectStatus'];
      if (Get.arguments['subDevicesIdList'] != null) {
        subDevicesIdList = Get.arguments['subDevicesIdList'];
      }
      if (Get.arguments['subDevicesNameList'] != null) {
        subDevicesNameList = Get.arguments['subDevicesNameList'];
      }

      model = DeviceTabModel();

      getTabPage().then((value) {
        Log.d("设备模块getTabPage");
        // pages = value;
        // tabController = TabController(vsync: this, length: 4);
        // tabController?.addListener(() {
        //   currentIndex = tabController?.index ?? 0;
        //   update();
        // });
        // update();
      });
    }
  }

  @override
  void onReady() {
    // Get.put(NewAirCoolingPageController());
  }

  @override
  void onClose() {
    tabController?.dispose();
    super.onClose();
  }

  void onItemTapped(int index) {
    currentIndex = index;
    // tabController?.animateTo(index);
    update();
  }

  Future<void> getTabPage() async {
    if (deviceDetailModel == null) {
      var json = await getDeviceDetail();
      deviceDetailModel = json == null ? null : DeviceModel.fromJson(json);
    }

    /// functionType 标识ems或空调   0矩形 1比瓦特 2自研

    detailWidget ??= model!.getDeviceDetailPage(
        deviceId: deviceId,
        deviceModel: deviceDetailModel,
        productIdType: productId,
        productType: type,
        subDevicesIdList: subDevicesIdList,
        subDevicesNameList: subDevicesNameList,
        deviceTree: deviceTree);
    alarmHomeWidget ??= AlarmHomePage(
      deviceId: deviceId,
      deviceName: deviceDetailModel?.name,
      tagName: '${deviceDetailModel?.name}/$deviceId',
      alarmPageType: AlarmPageType.device,
    );
    baseInfoWidget ??= BasicInfoPage(deviceId);

    YtTabViewModel model1 = YtTabViewModel(
      permissionString: PermissionConstant.deviceDetailDeviceDetail,
      labelString: ResKey.deviceDetail.tr,
      imageString: ImageAssets.deviceTabDetailUnselected,
      permissionWidget: KeepAliveWrapper(detailWidget!),
    );
    YtTabViewModel model2 = YtTabViewModel(
      permissionString: PermissionConstant.deviceDetailAlarm,
      labelString: ResKey.alarm.tr,
      imageString: ImageAssets.deviceTabAlarmUnselected,
      permissionWidget: KeepAliveWrapper(alarmHomeWidget!),
    );
    YtTabViewModel model3 = YtTabViewModel(
      permissionString: PermissionConstant.deviceDetailHistory,
      labelString: ResKey.deviceHistoricalInformatio.tr,
      imageString: ImageAssets.deviceTabHistoryUnselected,
      permissionWidget: const KeepAliveWrapper(HistoryInfoPage()),
    );
    YtTabViewModel model4 = YtTabViewModel(
      permissionString: PermissionConstant.deviceDetailBasicInfo,
      labelString: ResKey.basicInfo.tr,
      imageString: ImageAssets.deviceTabBasicUnselected,
      permissionWidget: KeepAliveWrapper(baseInfoWidget!),
    );
    permissionGroups.add(model1);
    permissionGroups.add(model2);
    permissionGroups.add(model3);
    permissionGroups.add(model4);
    update();
  }

  /// 获取设备详情
  Future getDeviceDetail() async {
    return await services.getDeviceInfo({"deviceId": deviceId}, onError: (err) {
      handleException(err);
      return false;
    });
  }
}
